import Vue from 'vue'
import Vuex from 'vuex'
import CoreApi from '@/libs/api'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userinfo: null,
    serverListData: []
  },
  getters: {
    GET_USERINFO(state) {
      return state.userinfo ?? JSON.parse(localStorage.getItem('userinfo'))
    }
  },
  mutations: {
    SET_SERVERLIST: (state, data) => {
      state.serverListData = data
    },
    SET_USERINFO: (state, data) => {
      state.userinfo = data
      localStorage.setItem('userinfo', JSON.stringify(data))
    },
    SET_LOGOUT: (state) => {
      state.userinfo = null
      localStorage.removeItem('userinfo')
    },
  },
  actions: {
    async getServer({ commit }) {
      commit('SET_SERVERLIST', await CoreApi.getserver())
    }
  },
  modules: {
  }
})
